// pages/brand/brand.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
import commonUtils from '../../utils/common.js';
import myUtils from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    brandBussList: [],
    brandList: [],//全部品牌
    brandsList: [],//推荐品牌
    showBrandSearch: true,
    areaId: '',
    keyWord: ''
  },
  /**
   * 选择商圈
   */
  chooseArea: function (e) {
    var that = this;
    var areaId = e.currentTarget.dataset.areaid;
    that.setData({
      areaId: areaId,
    })
    that.selectBrandList();
  },
  /**
   * 选择商圈
   */
  selectByBrandName: function (e) {
    var that = this;
    var keyWord = e.detail.value;
    that.setData({
      keyWord: keyWord,
    })
  },
  /**
   * 品牌列表
   */
  selectBrandList: function (e) {
    var that = this;
    var keyWord = that.data.keyWord;
    that.setData({
      keyWord: '',
      showBrandSearch: true,
    })
    httpsUtils.getData({
      _url: app.globalData._url + '/brand/selectList?status=1&tradeAreaId=' + that.data.areaId + "&keyWord=" + keyWord,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        var brands_list = json.data.slice(0, 4);
        that.setData({
          brandList: json.data,
          brandsList: brands_list,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /* 商圈列表 */
    httpsUtils.getData({
      _url: app.globalData._url + '/tradeArea/selectList',
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          brandBussList: json.data,
        })
      } else {

      }
    })

    /* 品牌列表 */
    that.selectBrandList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '服装基地-品牌大本营',
      path: 'pages/brand/brand',
    }
  },
  /*事件函数*/
  searchBrand: function () {
    this.setData({
      showBrandSearch: false,
    })
  },
  /*关闭搜索框*/
  closeSearch: function () {
    this.setData({
      showBrandSearch: true,
    })
  },
  getData: function (params, cb) {
    // console.log(params)
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
})